package com.zp.auth.controller;


import com.zp.auth.body.LoginBody;
import com.zp.auth.service.SysLoginService;
import com.zp.common.core.entity.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * 登录 token
 */


@Validated
// 生成带有必需参数的构造函数。 必需的参数是最终字段和具有约束的字段，例如@NonNull 。
@RequiredArgsConstructor
@RestController
public class TokenController {

    private final SysLoginService sysLoginService;

    /**
     * 登录方法
     */
    @PostMapping("login")
    public R<Map<String, Object>> login (@Validated @RequestBody LoginBody loginBody) {
        // 用户登录
        String accessToken = sysLoginService.login(loginBody.getUsername(), loginBody.getPassword());

        // 接口返回信息
        Map<String, Object> rspMap = new HashMap<String, Object>();
        rspMap.put("access_token", accessToken);
        return R.ok(rspMap);
    }

    /**
     * 登出方法
     */
    @DeleteMapping("logout")
    public R<Void> logout () {
        sysLoginService.logout();
        return R.ok();
    }
}
